package pro.ticktock.tools.mbconv;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lambert on 2017/5/28.
 */
public class Pass1 extends AbstractPass{ //process line-irrelevant errors

  Pattern numPattern = Pattern.compile("N[0oO][,，\\.][ ]*([0-9])");
  Pattern badOpNum1Pattern = Pattern.compile("[0O]P[,，。\\.．]([0-9])");
  Pattern badOpNum2Pattern = Pattern.compile("0P([0-9])");
  Pattern badOpNum3Pattern = Pattern.compile("[0O]P[,，。\\.．][ ]([0-9])");
  Pattern badWooNumPattern = Pattern.compile("WoO[,，。\\.．]([0-9])");
  Pattern badZeroPattern = Pattern.compile("[^\\d]+([0])[^\\d]+[\\d]+[\\.．]");

  public Pass1(String srcFileName) throws IOException {
    super(srcFileName);
  }

  public String correct(String line) throws IOException {

    Matcher m = numPattern.matcher(line);
    if (m.find()) {
      StringBuffer sb = new StringBuffer();
      int lastEnd = 0;
      do {
        sb.append(line.substring(lastEnd, m.start()));
        sb.append("No." + m.group(1));
        lastEnd = m.end();
      } while (m.find());
      sb.append(line.substring(lastEnd));
      //System.out.println("Correcting line with No.1: '" + line + "' with '" + sb + "'.");
      line = sb.toString();
    }

    line = line.replace("No.l", "No.1");

    m = badOpNum1Pattern.matcher(line);
    if (m.find()) {
      StringBuffer sb = new StringBuffer();
      int lastEnd = 0;
      do {
        sb.append(line.substring(lastEnd, m.start()));
        sb.append("OP." + m.group(1));
        lastEnd = m.end();
      } while (m.find());
      sb.append(line.substring(lastEnd));
      //System.out.println("Correcting line with OP.1: '" + line + "' with '" + sb + "'.");
      line = sb.toString();
    }

    m = badOpNum2Pattern.matcher(line);
    if (m.find()) {
      StringBuffer sb = new StringBuffer();
      int lastEnd = 0;
      do {
        sb.append(line.substring(lastEnd, m.start()));
        sb.append("OP" + m.group(1));
        lastEnd = m.end();
      } while (m.find());
      sb.append(line.substring(lastEnd));
      //System.out.println("Correcting line with OP1: '" + line + "' with '" + sb + "'.");
      line = sb.toString();
    }

    m = badOpNum3Pattern.matcher(line);
    if (m.find()) {
      StringBuffer sb = new StringBuffer();
      int lastEnd = 0;
      do {
        sb.append(line.substring(lastEnd, m.start()));
        sb.append("OP." + m.group(1));
        lastEnd = m.end();
      } while (m.find());
      sb.append(line.substring(lastEnd));
      //System.out.println("Correcting line with OP1: '" + line + "' with '" + sb + "'.");
      line = sb.toString();
    }

    line = line.replaceAll("W00|W0O|WO0", "WoO");

    m = badWooNumPattern.matcher(line);
    if (m.find()) {
      StringBuffer sb = new StringBuffer();
      int lastEnd = 0;
      do {
        sb.append(line.substring(lastEnd, m.start()));
        sb.append("WoO." + m.group(1));
        lastEnd = m.end();
      } while (m.find());
      sb.append(line.substring(lastEnd));
      //System.out.println("Correcting line with OP.1: '" + line + "' with '" + sb + "'.");
      line = sb.toString();
    }

    m = badZeroPattern.matcher(line);
    if (m.find()) {
      StringBuffer sb = new StringBuffer();
      int lastEnd = 0;
      do {
        sb.append(line.substring(lastEnd, m.start(1)));
        sb.append("。");
        lastEnd = m.end(1);
      } while (m.find());
      sb.append(line.substring(lastEnd));
      //System.out.println("Correcting line with OP.1: '" + line + "' with '" + sb + "'.");
      line = sb.toString();
    }

    if (line.length() > 0 && line.charAt(line.length() - 1) == '0') {
      logWriter.append("Suspicious '0' at end of line: " + line + "\n");
    }

    line = line.replaceAll("[　 ]*[，，][　 ]*", "，");
    line = line.replaceAll("[　 ]*[。][　 ]*", "。");
    //TODO through check
    //line = line.replaceAll("[‘’]", "");
    line = line.replaceAll("[(（][ ]+", "（");

    return line;
  }

  @Override
  public void doProcess() throws IOException {
      String line;
      while ((line = srcReader.readLine()) != null) {
        resultWriter.write(correct(line) + "\n");
      }
  }

  public static void main(String[] args) throws IOException {
    Pass1 p = new Pass1("merged.txt");
    p.process();
  }
}
